package com.jy.web.file.controller;


import com.jy.framework.utils.R;
import com.jy.web.file.service.impl.File2Url;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.LinkedList;

/**
 * @Author: orangy liu
 * @Description: 文件接口
 * @Class: ApiTaskController
 * @Date: 10:06 2018/9/28
 */
@Api(tags = "文件")
@RestController
@RequestMapping("/api/static/")
public class FileController {
    @ApiOperation("单文件上传")
    @PostMapping("/file/{businessType}")
    public R uploadFile(@RequestParam("file") MultipartFile file, @PathVariable(value = "businessType", required = false) String businessType) throws Exception {
        String s = File2Url.transferFile2Url(file, businessType);
        return R.ok().put(R.DATA, s);
    }
    @ApiOperation("多文件上传")
    @PostMapping("/files/{businessType}")
    public R uploadFiles(@RequestParam("files") MultipartFile[] files, @PathVariable(value = "businessType", required = false) String businessType) throws Exception {
        LinkedList<String> fileUrl =  new LinkedList<>();
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            String url = File2Url.transferFile2Url(file, businessType);
            fileUrl.add(url);
        }
        return R.ok().put(R.DATA, fileUrl);
    }

}
